package com.mifengs.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.MenuBean;
import com.mifengs.mall.entity.UpdateData;
import com.mifengs.mall.event.HomeFrgEvent;
import com.mifengs.mall.ui.web.CommonWebActivity;
import com.mifengs.mall.util.ToIntentUtil;
import com.mifengs.mall.widget.HorizontalPageLayoutManager;
import com.mifengs.mall.widget.IndicatorView;
import com.mifengs.mall.widget.PagingScrollHelper;
import com.youth.banner.Banner;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/11/1.
 */

public class ClassifyAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder>  {


    private Context mContext;

    private List<MenuBean> menuBeanList = new ArrayList<>();

    private MyItemClickListener myItemClickListener = new MyItemClickListener();

    PagingScrollHelper mScrollHelper = new PagingScrollHelper();

    public ClassifyAdapter(Context mContext) {
        this.mContext = mContext;
    }


    public void setMenuBeanList(List<MenuBean> menuBeanList) {
        this.menuBeanList = menuBeanList;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ClassifyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_classify,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ClassifyViewHolder){
//            final ClassifyViewHolder viewHolder = (ClassifyViewHolder) holder;
//            int size = menuBeanList.size();
//            int page = size/10;
//            if(size%10>0){
//                page = page+1;
//            }
//            HorizontalPageLayoutManager horizontalPageLayoutManager = new HorizontalPageLayoutManager(2,5);
//            viewHolder.mIndicatorView.setTotalSize(page);
//            viewHolder.mRvClassify.setLayoutManager(horizontalPageLayoutManager);
//            mScrollHelper.setUpRecycleView(viewHolder.mRvClassify);
//            mScrollHelper.setOnPageChangeListener(new PagingScrollHelper.onPageChangeListener() {
//                @Override
//                public void onPageChange(int index) {
//                    viewHolder.mIndicatorView.setCurrentIndex(index);
//                }
//            });
//            ClassifyRvAdapter adapter = new ClassifyRvAdapter();
//            viewHolder.mRvClassify.setAdapter(adapter);
//            adapter.setNewData(menuBeanList);
//            if(size!=0){
//                viewHolder.mRvClassify.addOnItemTouchListener(myItemClickListener);
//            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class ClassifyViewHolder extends RecyclerView.ViewHolder{

        RecyclerView mRvClassify;

        IndicatorView mIndicatorView;

        public ClassifyViewHolder(View itemView) {
            super(itemView);
            mRvClassify = (RecyclerView) itemView.findViewById(R.id.rv_classify);
            mIndicatorView = itemView.findViewById(R.id.id_view);
        }
    }

    class MyItemClickListener extends OnItemClickListener{

        @Override
        public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
            MenuBean bean = menuBeanList.get(position);
            if(bean.getId().equals("455")){
                EventBus.getDefault().post(new HomeFrgEvent(HomeFrgEvent.OPENSHOP));
            }else{
                ToIntentUtil.toCommonIntent(menuBeanList.get(position).getUrl(),mContext);
            }
        }
    }





}
